package com.d6game.test;


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.d6game.web.util.HttpClientUtil;

public class SystemUtil{
	
	public static void parseRecord(String detail) {
		Element root = SystemUtil.getDocumentElement(detail);
		
		NodeList nodes = root.getElementsByTagName("origin_airport");
		if(nodes != null && nodes.getLength() > 0) {
			String from = nodes.item(0).getTextContent();
			System.out.println(from);
		}
		nodes = root.getElementsByTagName("destination_airport");
		if(nodes != null && nodes.getLength() > 0) {
			String from  = nodes.item(0).getTextContent();		
			System.out.println(from);
		}
		nodes = root.getElementsByTagName("OP_TIME");
		if(nodes != null && nodes.getLength() > 0) {
			String from  = nodes.item(nodes.getLength() - 1).getTextContent();
			System.out.println(from);
		}
		nodes = root.getElementsByTagName("TRACE_DATA");
		if(nodes != null && nodes.getLength() > 0) {
			int i = nodes.getLength();
			String from  = nodes.item(nodes.getLength() - 1).getTextContent();
			System.out.println(from);
		}
		
	}
	
	public static boolean checkPassword(String password) {
		if(password.length() < 6) {
			return false;
		} else {
			return true;
		}
	}
	public static Element getDocumentElement(File file) {
		Element root = null;
		try {			
			if(file.exists()) {
				FileInputStream fin = new FileInputStream(file);
				byte[] data = new byte[fin.available()];
				fin.read(data);
				
				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
				DocumentBuilder builder = factory.newDocumentBuilder(); 
				Document doc = builder.parse(fin);
				
				root = doc.getDocumentElement();
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return root;
	}
	
	public static Element getDocumentElement(String xmlString) {
		Element root = null;
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder(); 
		     if (xmlString != null) {  
		             Pattern p = Pattern.compile("\\s*|\t|\r|\n");  
		             Matcher m = p.matcher(xmlString);  
		             xmlString = m.replaceAll("");  
		     } 
			InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8"));
			Document doc = builder.parse(is);
			
			root = doc.getDocumentElement();
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return root;
	}
	public static void main(String[] args) {
		
		System.out.println(HttpClientUtil.doPost("http://vote.tjqng.com/updateTVoteNew.jsp?voteId=181", ""));
	}
}
